//
//  STagImageView.m
//  EasyRetail
//
//  Created by Samuel on 16/1/6.
//
//

#import "STagImageView.h"
#import "UIImageView+WebCache.h"
#import "SDefine.h"
//#import "TagTemplateView.h"
@implementation STagImageView


+ (STagImageView*)shareSTagImageViewInitializationInFrame:(CGRect)frames
                                                 andModel:(STagImageModel*)model;
{
    STagImageView *views = [[STagImageView alloc]initWithFrame:frames];
    views.imageViews = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frames), CGRectGetHeight(frames))];
    views.imageViews.contentMode = UIViewContentModeScaleAspectFit;
    [views addSubview:views.imageViews];
    if (model != nil) {
        views.imageModel = model;
    }
    return views;
}


- (void)setImageModel:(STagImageModel *)imageModel
{
    _imageModel = imageModel;
    
    ___WEAKSELF
    [self.imageViews sd_setImageWithURL:[NSURL URLWithString:imageModel.imageUrl]
                       placeholderImage:[self drawImage]
                              completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                  if (image == nil) {
                                      [weakSelf.imageViews setImage:[UIImage imageNamed:@"无图片.png"]];
                                      return ;
                                  }
                                  /**
                                   *  处理坐标点
                                   */
                                  CGSize iSize = weakSelf.imageViews.image.size;
                                  NSMutableArray *newPointArray = [NSMutableArray array];
                                  for (NSString *pointString in imageModel.pointArray) {
                                      CGPoint pts = CGPointFromString(pointString);
                                      float x = pts.x*___width(weakSelf)/iSize.width;
                                      float y = pts.y*___height(weakSelf)/iSize.height;
                                      NSString *newPointString = NSStringFromCGPoint(CGPointMake(x, y));
                                      [newPointArray addObject:newPointString];
                                  }
                                  
                                  for (int i = 0; i<newPointArray.count; i++) {
                                      CGPoint gp = CGPointFromString(newPointArray[i]);
//                                      TagModel *tModel = [[TagModel alloc]init];
//                                      tModel.point = NSStringFromCGPoint(gp);
//                                      NSDictionary *getdic = imageModel.objectArray[i];
//                                      tModel.goodName = [getdic objectForKey:@"Title"];
//                                      tModel.orientation = ([[getdic objectForKey:@"TagType"] integerValue]==1)?left_Orientation:right_Orientation;
//                                      TagTemplateView *tagTemp = [TagTemplateView initializeTagTemplateViewWithTagModel:tModel];
//                                      tagTemp.closeAction = YES;
//                                      [weakSelf addSubview:tagTemp];
                                  }
    }];
}
- (UIImage *)drawImage {
    UIImage *centerImage = [UIImage imageNamed:@"loading_img"];
    UIImage *baseImage;

    CGFloat drawWidth = ___width(self.imageViews);
    CGFloat drawHeight = ___height(self.imageViews);
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(drawWidth, drawHeight), NO, 1);
    UIBezierPath* p = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,drawWidth,drawHeight)];
    [[UIColor whiteColor] setFill];
    [p fill];
    [baseImage drawInRect:CGRectMake(0, 0, drawWidth, drawHeight)];
    [centerImage drawInRect:CGRectMake(drawWidth/2-centerImage.size.width/2, drawHeight/2-centerImage.size.height/2, centerImage.size.width, centerImage.size.height)];
    baseImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return baseImage;
}

@end
@implementation STagImageModel
@end